package com.ruoyi.question.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.question.domain.QQuestion;
import com.ruoyi.question.enums.QuestionTypeEnum;
import com.ruoyi.question.mapper.QQuestionMapper;
import com.ruoyi.question.service.IQQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 题目Service业务层处理
 *
 * @author zouhao
 * @date 2024-09-11
 */
@Service
public class QQuestionServiceImpl extends ServiceImpl<QQuestionMapper, QQuestion> implements IQQuestionService {
    @Autowired
    private QQuestionMapper qQuestionMapper;

    @Override
    public void importData(InputStream inputStream) {
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        List<List<Object>> readAll = reader.read();
        List<QQuestion> questionList=new ArrayList<>();
        StringBuffer sqlStr=new StringBuffer();
        for(int i=1;i<readAll.size();i++){
            if(readAll.get(i).get(2)==null){
                continue;
            }
            QQuestion question=new QQuestion();
            question.setQuestionName(readAll.get(i).get(1).toString().trim());
            switch (readAll.get(i).get(2).toString()){
                case "单选题":
                    question.setQuestionType(QuestionTypeEnum.SINGLE);
                    break;
                case "多选题":
                    question.setQuestionType(QuestionTypeEnum.MULTIPLE);
                    break;
                case "判断题":
                    question.setQuestionType(QuestionTypeEnum.JUDGE);
                    break;
                case "不定项选择":
                    question.setQuestionType(QuestionTypeEnum.SINGLE_OR_MULTIPLE);
                    break;
//                default:
//                    throw new Exception("第"+i+"行有问题");
            }
            if(readAll.get(i).get(2).toString().equals("判断题")){
                question.setA("对");
                question.setB("错");
            }else{
                question.setA(readAll.get(i).get(3).toString().trim());
                question.setB(readAll.get(i).get(4).toString().trim());
                question.setC(readAll.get(i).get(5).toString().trim());
                question.setD(readAll.get(i).get(6).toString().trim());
                question.setE(readAll.get(i).get(7)==null?"":readAll.get(i).get(7).toString().trim());
                question.setF(readAll.get(i).get(8)==null?"":readAll.get(i).get(8).toString().trim());
            }
            question.setAnswer(readAll.get(i).get(9).toString().toUpperCase().trim());
            question.setSort(i);
            question.setCreateTime(new Date());
            question.setUpdateTime(new Date());
            questionList.add(question);
        }
        this.getBaseMapper().truncateTable();
        this.saveBatch(questionList);
    }
}
